<?php
 class Warranty extends CI_Controller{
 	public function __construct(){
 		parent::__construct();
 	}
 	
 	public function warranty_list($str="",$imie=""){
 		if($str !="" && $imie !=""){
 			$arr = array();
 			$this->db->select("t1.model_no,t1.serial_no,t1.purchase_date");
	 		$this->db->from("mobile_warranty t1");
	 		$this->db->join("mobile_member t2", "t1.member_pkid=t2.pkid");
	 		$this->db->join("mobile_customer t3", "t2.customer_pkid=t3.pkid");
	 		$this->db->where("t2.imei",$imie);
	 		$this->db->where("t3.name",$str);
	 		$this->db->order_by("t1.pkid","desc");
	 		$res = $this->db->get()->result_array();
	 		foreach ($res as $P){
	 			$tmp['model']				=$P['model_no'];
	 			$tmp['serial']					=$P['serial_no'];
	 			$tmp['purchase_date']	=$P['purchase_date'];
	 			$arr[] = $tmp;
	 		}
	 		echo json_encode($arr);
 		}
 	}
 	
 	public function warranty_add($str=""){
 		if($str != ""){
 			$arrs = array();
 			 $this->db->select("t1.pkid");
 			 $this->db->from("mobile_member t1");
 			 $this->db->join("mobile_customer t2", "t1.customer_pkid=t2.pkid");
 			 $this->db->where("t1.imei",$this->input->post("imie"));
 			 $this->db->where("t2.name",$str);
 			 $rows = $this->db->get()->row();
 			 //== add warranty 
 			 $data = array(
 			 		"member_pkid"=>$rows->pkid,
 			 		"model_no"=>$this->input->post("id_no"),
 			 		"serial_no"=>$this->input->post("serial"),
 			 		"purchase_date"=>$this->input->post('purchase')
 			 		);
 			 $res =$this->db->insert("mobile_warranty",$data);
 			 if($res){
 			 	$arrs['StatusID']  	= "1";
 			 	$arrs['Error']			="";
 			 }else{
 			 	$arrs['StatusID']  	= "0";
 			 	$arrs['Error']			="Add Warranty Fail!";
 			 }
 			
 		}else{
 			$arrs['StatusID']  	= "0";
 			$arrs['Error']			=" Fail!";
 		}
 		echo json_encode($arrs);
 	}
 	
 	public function warranty_check($str="",$imie=""){
 		if($str !="" && $imie !=""){
 			$arr = array();
 			$this->db->select("t1.model_no,t1.serial_no,t1.purchase_date");
	 		$this->db->from("mobile_warranty t1");
	 		$this->db->join("mobile_member t2", "t1.member_pkid=t2.pkid");
	 		$this->db->join("mobile_customer t3", "t2.customer_pkid=t3.pkid");
	 		$this->db->where("t2.imei",$imie);
	 		$this->db->where("t3.name",$str);
	 		$this->db->where("t1.status","1");
	 		$this->db->order_by("t1.pkid","desc");
	 		$res = $this->db->get()->result_array();
	 		foreach ($res as $P){
	 			$tmp['model']				=$P['model_no'];
	 			$arr[] = $tmp;
	 		}
	 		echo json_encode($arr);
 		}
 	}
 	
 	public function tracking_check($str="",$imie=""){
 		//$str = "huawei";
 		//$imie = "358867040228009";
 		if($str !="" && $imie !=""){
 			$arr = array();
 			$this->db->select("t1.model_no,t1.pkid,t1.return_date");
 			$this->db->from("mobile_warranty_tracking t1");
 			$this->db->join("mobile_member t2", "t1.member_pkid=t2.pkid");
 			$this->db->join("mobile_customer t3", "t2.customer_pkid=t3.pkid");
 			$this->db->where("t2.imei",$imie);
 			$this->db->where("t3.name",$str);
 			$this->db->order_by("t1.pkid","desc");
 			$res = $this->db->get()->result_array();
 			foreach ($res as $P){
 				$tmp['pkid']							=$P['pkid'];
 				$tmp['model']						=$P['model_no'];
 				$tmp['return_date']				=$P['return_date'];
 				$arr[] = $tmp;
 			}
 			echo json_encode($arr);
 		}
 	}
 	
 	public function tracking_detail($pkid=""){
 		$tmp = array();
 		$arr  = array();
 		if($pkid != ""){
 			$this->db->where("pkid",$pkid);
 			$rows = $this->db->get("mobile_warranty_tracking")->row();
 			$tmp['tpkid']				=$rows->pkid;
 			$tmp['model']			=$rows->model_no;
 			$tmp['serial']				=$rows->serial_no;
 			$tmp['return_date']	=$rows->return_date;
 			$tmp['remark']			=$rows->remark;
 			$arr[]		=$tmp;
 		}
 		echo json_encode($arr);
 	}
 	
 	public function tracking_update($pkid){
 		$arr = array();
 		if($pkid != ""){
 			$this->db->where("pkid",$pkid);
 			$res = $this->db->update(
 					"mobile_warranty_tracking",
 					array("remark_return"=>$this->input->post("feedback"))
 					);
 			if($res){
 				$arr['StatusID']  ="1";
 				$arr['Error']		="Update success";
 			}else{
 				$arr['StatusID']  ="0";
 				$arr['Error']		="Update Fail";
 			}
 		}else{
 			$arr['StatusID']  ="0";
 			$arr['Error']		="Fail";
 		}
 		echo json_encode($arr);
 	}
 	
 	public function campaign($str=""){
 		$arr = array();
 		if($str != ""){
 			$this->db->select("t1.picture");
 			$this->db->where("t2.name",$str);
 			$this->db->from("mobile_campaign t1");
 			$this->db->join("mobile_customer t2", "t1.customer_pkid=t2.pkid");
 			$this->db->order_by("t1.pkid",'desc');
 			$this->db->limit(1);
 			$rows = $this->db->get()->row();
 			$tmp['img']		=$rows->picture;
 			$arr[]		=$tmp;
 		}
 		echo json_encode($arr);
 	}
 	
 }
?>